package cn.bluethink.eguan.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 通用工具类
 * 
 * @title 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.1.0
 * @describe
 * @author liuxn
 * @date 2018年8月22日
 */
public class DateUtil {

    /**
     * 根据日期（忽略时分秒）比较大小
     * 
     * @param date
     * @param newDate
     * @return
     */
    public static Integer dateCompare(Date date, Date newDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        String dateFirst = dateFormat.format(date);
        String dateLast = dateFormat.format(newDate);

        int dateFirstIntVal = Integer.parseInt(dateFirst);
        int dateLastIntVal = Integer.parseInt(dateLast);

        if (dateFirstIntVal > dateLastIntVal) {
            return 1;
        } else if (dateFirstIntVal < dateLastIntVal) {
            return -1;
        }
        return 0;
    }

    /**
     * 将日期转成字符串 wanglei于2018/11/14添加
     * 
     * @param date
     * @return
     * @throws Exception
     */
    public static String formatDate(Date date, String type) throws Exception {
        if (date == null)
            return null;
        SimpleDateFormat format = null;
        if (type.equals("时分秒")) {
            format = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
        } else {
            format = new SimpleDateFormat("yyyy-MM-dd");
        }
        return format.format(date);
    }

    /**
     * 当前日期+num天
     * 
     * @param date
     * @param num
     * @return
     */
    public static Date addDay(Date date, int num) {
        Calendar startDT = Calendar.getInstance();
        startDT.setTime(date);
        startDT.add(Calendar.DATE, num);
        return startDT.getTime();
    }

    /**
     * 获取当前日期23点59分59秒
     * 
     * @param date
     * @return
     */
    public static Date dateTime(Date date, int hour, int minute, int second) {
        // 获取当天23点59分59秒Date
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                hour, minute, second);
        calendar1.set(Calendar.MILLISECOND, 0);
        return calendar1.getTime();
    }

    /**
     * 将起止时间转换为字符串
     * 
     * @param bdate
     * @param edate
     * @return
     * @throws Exception
     */
    public static List<String> dateToString(Date bdate, Date edate) throws Exception {
        List<String> dateStr = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String begin = sdf.format(bdate);
        String end = sdf.format(edate);
        StringBuffer bDateSb = new StringBuffer().append(begin).append(" 00:00:00");
        StringBuffer eDateSb = new StringBuffer().append(end).append(" 23:59:59");
        dateStr.add(bDateSb.toString());
        dateStr.add(eDateSb.toString());
        return dateStr;
    }
}
